function ajax_tool(method, url, data) {
    return new Promise(function (resolve, reject) {

        let xhr = new XMLHttpRequest()
        if (method == "GET") {
            let newUrl = url + '?'
            for (let key in data) {
                newUrl += `${key}=${data[key]}&`
            }
            newUrl = newUrl.slice(0, -1)
            xhr.open(method, newUrl)
            xhr.send()
        } else {
            let postData = ""
            for (let key in data) {
                postData += `${key}=${data[key]}&`
            }
            postData = postData.slice(0, -1)
            xhr.open(method, url);
            xhr.setRequestHeader('content-Type', 'application/x-www-form-urlencoded')
            xhr.send(postData);
        }
        xhr.addEventListener('readystatechange', function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                } else {
                    reject('请求失败了,请检查参数或地址是否有误')
                }
            }
        })

    })
}
export { ajax_tool }